package com.green.dao;

import com.green.domain.Message;
import com.green.domain.User;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by WHUER on 2016/6/12 0012.
 */
@Repository
public class MessageDao extends BaseDao<Message> {
    protected final java.lang.String GET_MESSAGE_DETAIL = " from Message m where m.ID=?";
    protected final String GET_MESSAGE_NUM = "SELECT count(m.ID) from Message m where m.userTo=?";
    protected final String GET_ALL_MY_MSG = "FROM Message m where m.userTo=?";


    /**
     * 获取某条消息的内容
     *
     * @param id
     * @return
     */
    public Message getMessageDetail(int id) {
        Iterator iterator = getHibernateTemplate().iterate(GET_MESSAGE_DETAIL, id);

        return (Message) iterator.next();
    }

    /**
     * 这里需要使用Long类型的返回值
     *
     * @param user
     * @return
     */
    public Long getMessageNum(User user) {
        Iterator iterator = getHibernateTemplate().iterate(GET_MESSAGE_NUM, user.getUserId());
        return (Long) iterator.next();
    }

    /**
     * 返回某个用户{user}的所有的消息内容
     *
     * @param user： 当前用户
     * @return: 返回输入用户的消息内容
     */
    public List<Message> getMyMessage(User user) {
        List list = (List) getHibernateTemplate().find(GET_ALL_MY_MSG, user.getUserId());
        return list;
    }

}
